package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/*
*完成用户注册功能
* 程序启动后要求用户在控制台上陆续输入：用户名，密码，昵称，年龄
* 输入完毕后，将用户输入的数据插入到数据库userinfo中完成注册
 */
public class RegDemo {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        System.out.println("请输入用户名");
        Scanner sc = new Scanner(System.in);
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        System.out.println("请输入昵称");
        String nickname = sc.nextLine();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true",
                "root",
                "root");
        System.out.println("与数据库建立连接");

        Statement stmt = connection.createStatement();
        String sql = "insert into userinfo(username,password,nickname,age) values('"+username+"','"+password+"','"+nickname+"',"+age+")";
        stmt.executeUpdate(sql);
        System.out.println("注册成功");
        stmt.close();
        connection.close();

    }
}
